<?php


namespace App\Http\Controllers\WebApi;


use App\Http\Controllers\WebApi\Business\FriendsCircleBusiness;
use App\Http\Controllers\WebApi\Business\OperationLogsBusiness;
use App\Models\ImFriendsCircle;
use App\Models\ImFriendsCircleComments;
use App\Models\ImFriendsCirclePraise;
use App\Models\ImOperationLogs;
use Illuminate\Http\Request;

class FriendsCircleController
{
    public function __construct()
    {
        $this->fcBusiness = new FriendsCircleBusiness();
    }

    /**
     * 朋友圈列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-15 10:25
     */
    public function fcList(Request $request)
    {
        $searchWord = $request->get('searchWord');
        list($offset, $limit, $page, $pagesize) = ImFriendsCircle::pageProcess();
        $query = ImFriendsCircle::with(['user', 'imgs', 'province', 'city', 'area']);
        if ($searchWord) {
            $query->whereHas('user', function ($sql) use ($searchWord) {
                $sql->where('id', $searchWord)->orWhere('nickname', 'like', $searchWord . '%');
            });
        }
        $total = $query->count();
        $data = $query->offset($offset)->limit($limit)->orderBy('id', 'desc')->get();
        $data = ImFriendsCircle::addressPress($data);
        return okBack($data, [
            'total' => $total,
            'page' => $page,
            'pagesize' => $pagesize,
        ]);
    }

    /**
     * 点赞操作
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-15 10:25
     */
    public function praise(Request $request)
    {
        $id = $request->post('id');
        $friend_id = $request->post('friend_id');
        if ($id <= 0 || $friend_id <= 0) {
            return errBack('参数错误');
        }
        $res = $this->fcBusiness->praise($id, $friend_id);
        if ($res['code'] !== 0) {
            return errBack('点赞失败' . $res['msg']);
        }
        return okBack();
    }

    /**
     * 朋友圈点赞列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-15 10:25
     */
    public function praiseList(Request $request)
    {
        $id = $request->get('id');
        if ($id <= 0) {
            return errBack('参数错误');
        }
        $fcData = ImFriendsCircle::find($id);
        if (!$fcData) {
            return errBack('信息不存在');
        }
        list($offset, $limit, $page, $pagesize) = ImFriendsCirclePraise::pageProcess();
        $query = ImFriendsCirclePraise::with(['user'])->where('fc_id', $id)->whereHas('user');
        $total = $query->count();
        $fcpData = $query->offset($offset)->limit($limit)->get();
        return okBack($fcpData, [
            'total' => $total,
            'page' => $page,
            'pagesize' => $pagesize,
        ]);
    }

    /**
     * 朋友圈评论列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-15 10:25
     */
    public function commentList(Request $request)
    {
        $id = $request->get('id');
        if ($id <= 0) {
            return errBack('参数错误');
        }
        $fcData = ImFriendsCircle::find($id);
        if (!$fcData) {
            return errBack('信息不存在');
        }
        list($offset, $limit, $page, $pagesize) = ImFriendsCircleComments::pageProcess();
        $query = ImFriendsCircleComments::with(['user'])->where('fc_id', $id)->whereHas('user');
        $total = $query->count();
        $fcpData = $query->offset($offset)->limit($limit)->get();
        return okBack($fcpData, [
            'total' => $total,
            'page' => $page,
            'pagesize' => $pagesize,
        ]);
    }

    /**
     * 删除朋友圈
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-15 10:26
     */
    public function del(Request $request)
    {
        $id = $request->post('id');
        if ($id <= 0) {
            return errBack('参数错误');
        }
        $res = ImFriendsCircle::destroy($id);
        if ($res) {
            OperationLogsBusiness::addLog(ImOperationLogs::DELETE, '删除朋友圈;ID:' . $id);
        }
        return okBack();
    }

    /**
     * 单个或批量屏蔽
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-15 10:30
     */
    public function shield(Request $request)
    {
        $fcId = $request->post('fcId');
        $fcId = json_decode($fcId, true);
        if (empty($fcId)) {
            return errBack();
        }
        ImFriendsCircle::whereIn('id', $fcId)->update([
            'status' => 2,
            'remark' => '管理后台审核不通过'
        ]);
        return okBack();
    }

    /**
     * 单个或批量解除屏蔽
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-15 10:34
     */
    public function unshield(Request $request)
    {
        $fcId = $request->post('fcId');
        $fcId = json_decode($fcId, true);
        if (empty($fcId)) {
            return errBack();
        }
        ImFriendsCircle::whereIn('id', $fcId)->update([
            'status' => 1
        ]);
        return okBack();
    }
}
